//
//  GSCommonLoginInputView.m
//  GSLoginDemo
//
//  Created by guoshikeji_a on 2018/7/27.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "GSCommonLoginInputView.h"

@interface GSCommonLoginInputView () <UITextFieldDelegate>


@end

@implementation GSCommonLoginInputView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
        [self.showPassButton sendActionsForControlEvents:UIControlEventTouchUpInside];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputViewTextFieldDidChangedNotification) name:UITextFieldTextDidChangeNotification object:self.accountTF];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputViewTextFieldDidChangedNotification) name:UITextFieldTextDidChangeNotification object:self.passwordTF];
    }
    return self;
}

- (void)setupSubviews {
    self.accountTF = [UITextField new];
    self.accountTF.textColor = GSLOGINRGBRGBHEX(0x000000);
    self.accountTF.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.accountTF.autocorrectionType = UITextAutocorrectionTypeNo;
    self.accountTF.keyboardType = UIKeyboardTypeEmailAddress;
    [self addSubview:self.accountTF];
    [self.accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(102.0/2);
        make.top.mas_equalTo(self);
        make.right.mas_equalTo(self).offset(-40.0);
        make.height.mas_equalTo(self).multipliedBy(.5);
    }];
    self.accountPlaceholder = GSLoginLocalized(@"Email address/phone number");
    //
    self.accountTF.inputAccessoryView = [UIView new];
    //
    self.passwordTF = [UITextField new];
    self.passwordTF.textColor = GSLOGINRGBRGBHEX(0x000000);
    self.passwordTF.delegate = self;
    [self addSubview:self.passwordTF];
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.accountTF);
        make.top.mas_equalTo(self.accountTF.mas_bottom);
        make.width.mas_equalTo(self.accountTF);
        make.height.mas_equalTo(self).multipliedBy(.5);
    }];
    self.passwordPlaceholder = GSLoginLocalized(@"Password");
    self.passwordTF.inputAccessoryView = [UIView new];
    //
    self.accountIconIV = [UIImageView new];
    self.accountIconIV.image = [UIImage imageNamed:@"gscommon_login_account"];
    [self addSubview:self.accountIconIV];
    [self.accountIconIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.accountTF.mas_left).offset(-18.0);
        make.centerY.mas_equalTo(self.accountTF);
    }];
    //
    self.passwordIconIV = [UIImageView new];
    self.passwordIconIV.image = [UIImage imageNamed:@"gscommon_login_password"];
    [self addSubview:self.passwordIconIV];
    [self.passwordIconIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.passwordTF.mas_left).offset(-18.0);
        make.centerY.mas_equalTo(self.passwordTF);
    }];
    //
    self.showPassButton = [QMUIButton new];
    self.showPassButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 20, 10);
    [self addSubview:self.showPassButton];
    [self.showPassButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self);
        make.centerY.mas_equalTo(self.passwordTF);
    }];
    [self.showPassButton addTarget:self action:@selector(showPassButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    //
    UIView *sepOne = [UIView new];
    sepOne.backgroundColor = GSLOGINRGBRGBHEX(0xF3F3F3);
    [self addSubview:sepOne];
    UIView *sepTwo = [UIView new];
    sepTwo.backgroundColor = GSLOGINRGBRGBHEX(0xF3F3F3);
    [self addSubview:sepTwo];
    [sepOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(10.0);
        make.bottom.mas_equalTo(self.accountTF);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(2);
    }];
    [sepTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(10.0);
        make.bottom.mas_equalTo(self.passwordTF);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(2);
    }];
}


- (void)setAccountPlaceholder:(NSString *)accountPlaceholder {
    _accountPlaceholder = accountPlaceholder;
    self.accountTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:accountPlaceholder attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18 weight:UIFontWeightRegular],NSForegroundColorAttributeName:GSLOGINRGBRGBHEX(0xD9D9D9)}];
}
- (void)setPasswordPlaceholder:(NSString *)passwordPlaceholder {
    _passwordPlaceholder = passwordPlaceholder;
    self.passwordTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:passwordPlaceholder attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18 weight:UIFontWeightRegular],NSForegroundColorAttributeName:GSLOGINRGBRGBHEX(0xD9D9D9)}];
}

- (NSString *)account {
    return self.accountTF.text;
}
- (NSString *)password {
    return self.passwordTF.text;
}

#pragma mark - showPassButtonDidClickedAction
- (void)showPassButtonDidClickedAction {
    [self.passwordTF resignFirstResponder];
    self.passwordTF.secureTextEntry = !self.passwordTF.secureTextEntry;
    if (self.passwordTF.secureTextEntry) {
        [self.showPassButton setImage:[UIImage imageNamed:@"gscommon_login_password_close"] forState:UIControlStateNormal];
    }else {
        [self.showPassButton setImage:[UIImage imageNamed:@"gscommon_login_password_open"] forState:UIControlStateNormal];
    }
}

#pragma mark - UITextFieldDelegate
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    //string就是此时输入的那个字符
//    //得到输入框的内容
//    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
//    if (textField == self.passwordTF && textField.isSecureTextEntry ) {
//        textField.text = toBeString;
//        return NO;
//    }
//    return YES;
//}

#pragma mark - noti
- (void)inputViewTextFieldDidChangedNotification {
    BOOL valid = self.account.length&&self.password.length;
    if (self.inputViewInvalidBlock) {
        self.inputViewInvalidBlock(valid);
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

+ (CGFloat)defaultHeight {
    return 112.0;
}

@end
